package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.*;
import com.macro.mall.mapper.BmsNarratorThemeApplicantMapper;
import com.macro.mall.mapper.BmsNarratorThemeMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.service.BmsNarratorThemeApplicantService;
import com.macro.mall.portal.util.MemberUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class BmsNarratorThemeApplicantServiceImpl extends ActivitiesBaseServiceImpl implements BmsNarratorThemeApplicantService {

    private static final Logger log = LoggerFactory.getLogger(BmsNarratorThemeApplicantServiceImpl.class);
    @Autowired
    private BmsNarratorThemeApplicantMapper bmsNarratorThemeApplicantMapper;

    @Autowired
    private BmsNarratorThemeMapper bmsNarratorThemeMapper;

    @Override
    public List<BmsNarratorThemeApplicant> getList(BmsNarratorThemeApplicantParam bmsNarratorThemeApplicantParam) {
        if (Objects.isNull(bmsNarratorThemeApplicantParam.getNarratorThemeId())) {
            return Lists.newArrayList();
        }
        Integer pageNum = bmsNarratorThemeApplicantParam.getPageNum();
        Integer pageSize = bmsNarratorThemeApplicantParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }

        BmsNarratorThemeApplicantExample example = new BmsNarratorThemeApplicantExample();
        BmsNarratorThemeApplicantExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        criteria.andNarratorThemeIdEqualTo(bmsNarratorThemeApplicantParam.getNarratorThemeId());
        if (StringUtils.isNotBlank(bmsNarratorThemeApplicantParam.getUserName())) {
            criteria.andUserNameLike("%" + bmsNarratorThemeApplicantParam.getUserName() + "%");
        }
        if (StringUtils.isNotBlank(bmsNarratorThemeApplicantParam.getUserPhone())) {
            criteria.andUserPhoneLike("%" + bmsNarratorThemeApplicantParam.getUserPhone() + "%");
        }
        if (Objects.nonNull(bmsNarratorThemeApplicantParam.getUserIdType())) {
            criteria.andUserIdTypeEqualTo(bmsNarratorThemeApplicantParam.getUserIdType());
        }
        if (StringUtils.isNotBlank(bmsNarratorThemeApplicantParam.getUserIdCard())) {
            criteria.andUserIdCardLike("%" + bmsNarratorThemeApplicantParam.getUserIdCard() + "%");
        }
        if (Objects.nonNull(bmsNarratorThemeApplicantParam.getApplicationTime())) {
            criteria.andApplicationTimeGreaterThanOrEqualTo(bmsNarratorThemeApplicantParam.getApplicationTime());
        }
        if (StringUtils.isNotBlank(bmsNarratorThemeApplicantParam.getStatus())) {
            criteria.andStatusEqualTo(bmsNarratorThemeApplicantParam.getStatus());
        }

        List<BmsNarratorThemeApplicant> list = bmsNarratorThemeApplicantMapper.selectByExample(example);
        return list;
    }

    @Override
    public BmsNarratorThemeApplicantDetail getItem(Long id) {
        BmsNarratorThemeApplicant bmsNarratorThemeApplicant = bmsNarratorThemeApplicantMapper.selectByPrimaryKey(id);
        BmsNarratorThemeApplicantDetail detail = new BmsNarratorThemeApplicantDetail();
        BeanUtils.copyProperties(bmsNarratorThemeApplicant, detail);
        return detail;
    }

    @Override
    public List<BmsNarratorThemeApplicant> findGroupApplicantByNarratorId(BmsNarratorThemeParam bmsNarratorThemeParam) {
        return bmsNarratorThemeApplicantMapper.findGroupApplicantByNarratorId(bmsNarratorThemeParam);
    }

    @Override
    Boolean checkBaseInfo(ActivitiesRegistrationParam registrationParam) {
        BmsNarratorThemeApplicantExample example = new BmsNarratorThemeApplicantExample();
        BmsNarratorThemeApplicantExample.Criteria criteria = example.createCriteria().andIsDeleteEqualTo((byte) 0).andNarratorThemeIdEqualTo(Long.valueOf(registrationParam.getActivitiesId()))
                .andUserIdEqualTo(registrationParam.getWxUser().getId()).andStatusEqualTo("0");
        List<BmsNarratorThemeApplicant> bmsNarratorThemeApplicants = bmsNarratorThemeApplicantMapper.selectByExample(example);
        bmsNarratorThemeApplicants = bmsNarratorThemeApplicants.stream().filter(i -> i.getApplicationTime().toLocalDate().isEqual(registrationParam.getApplicationTime().toLocalDate())).collect(Collectors.toList());
        return CollectionUtil.isEmpty(bmsNarratorThemeApplicants);
    }

    @Override
    int getActivitiesRegistratyCount(ActivitiesRegistrationParam registrationParam) {
        return registrationParam.getApplicantCount();
    }

    @Override
    int getActivitiesHasRegistratyCount(ActivitiesRegistrationParam registrationParam) {
        BmsNarratorThemeApplicantExample example = new BmsNarratorThemeApplicantExample();
        BmsNarratorThemeApplicantExample.Criteria criteria = example.createCriteria().andIsDeleteEqualTo((byte) 0).andNarratorThemeIdEqualTo(Long.valueOf(registrationParam.getActivitiesId()))
                .andUserIdEqualTo(registrationParam.getWxUser().getId()).andStatusEqualTo("0");
        List<BmsNarratorThemeApplicant> bmsNarratorThemeApplicants = bmsNarratorThemeApplicantMapper.selectByExample(example);
        long count = bmsNarratorThemeApplicants.stream().filter(i -> i.getApplicationTime().toLocalDate().isEqual(registrationParam.getApplicationTime().toLocalDate())).count();
        return Math.toIntExact(count);
    }

    @Override
    ActivitiesRegistrationDetail getActivitiesDetail(String activitiesId) {
        BmsNarratorThemeExample example = new BmsNarratorThemeExample();
        BmsNarratorThemeExample.Criteria criteria = example.createCriteria().andIsDeleteEqualTo((byte) 0).andIdEqualTo(Long.valueOf(activitiesId));
        List<BmsNarratorTheme> narratorThemeList = bmsNarratorThemeMapper.selectByExample(example);
        ActivitiesRegistrationDetail detail = new ActivitiesRegistrationDetail();
        detail.setApplicantCount(narratorThemeList.get(0).getPeopleNumber());
        detail.setCost(narratorThemeList.get(0).getCost());
        return detail;
    }

    @Override
    int insetApplicantList(ActivitiesRegistrationParam registrationParam) {
        UmsMember wxUser = registrationParam.getWxUser();
        LocalDateTime applicationTime = registrationParam.getApplicationTime();
        String activitiesId = registrationParam.getActivitiesId();
        BmsNarratorThemeApplicant bmsNarratorThemeApplicant = new BmsNarratorThemeApplicant();
        bmsNarratorThemeApplicant.setNarratorThemeId(Long.valueOf(activitiesId));
        bmsNarratorThemeApplicant.setPeopleNumber(registrationParam.getApplicantCount());
        bmsNarratorThemeApplicant.setApplicationTime(applicationTime);
        bmsNarratorThemeApplicant.setUserId(wxUser.getId());
        bmsNarratorThemeApplicant.setUserPhone(wxUser.getPhone());
        bmsNarratorThemeApplicant.setUserName(wxUser.getUsername());
        bmsNarratorThemeApplicant.setStatus("0");
        bmsNarratorThemeApplicant.setIsDelete((byte) 0);
        bmsNarratorThemeApplicant.setCreateUser(wxUser.getId());
        bmsNarratorThemeApplicant.setCreateTime(new Date());
        bmsNarratorThemeApplicant.setUpdateUser(wxUser.getId());
        bmsNarratorThemeApplicant.setUpdateTime(new Date());
        return bmsNarratorThemeApplicantMapper.insertSelective(bmsNarratorThemeApplicant);
    }

    @Override
    int handleApplicantStatus(ActivitiesRegistrationParam registrationParam, String status) {
        BmsNarratorThemeApplicantExample example = new BmsNarratorThemeApplicantExample();
        BmsNarratorThemeApplicantExample.Criteria criteria = example.createCriteria().andIsDeleteEqualTo((byte) 0).andNarratorThemeIdEqualTo(Long.valueOf(registrationParam.getActivitiesId()))
                .andUserIdEqualTo(registrationParam.getWxUser().getId()).andStatusEqualTo("0");
        List<BmsNarratorThemeApplicant> bmsNarratorThemeApplicants = bmsNarratorThemeApplicantMapper.selectByExample(example);
        List<Long> idList = bmsNarratorThemeApplicants.stream().filter(i -> i.getApplicationTime().toLocalDate().isEqual(registrationParam.getApplicationTime().toLocalDate())).map(BmsNarratorThemeApplicant::getId).collect(Collectors.toList());

        BmsNarratorThemeApplicant bmsNarratorThemeApplicant = new BmsNarratorThemeApplicant();
        bmsNarratorThemeApplicant.setStatus(status);

        BmsNarratorThemeApplicantExample updateExample = new BmsNarratorThemeApplicantExample();
        updateExample.createCriteria().andIdIn(idList);

        return bmsNarratorThemeApplicantMapper.updateByExampleSelective(bmsNarratorThemeApplicant,updateExample);
    }

    @Override
    public List<BmsNarratorThemeApplicant> selectByExample(BmsNarratorThemeApplicantExample example) {
        return bmsNarratorThemeApplicantMapper.selectByExample(example);
    }
}
